package com.bootcamp.javase.file;

import java.io.File;

/**
 * @program: FIleCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-08 14:11
 **/

public class DeleteDir {
    public static void main(String[] args) {
//        File dir = new File("../object/firstLevel");
//        System.out.println(dir.exists());
//        FileUtils.printDirResur(dir, 0);
//        FileUtils.deleteDirRecur(dir);
//        System.out.println("\n");
//        System.out.println(dir.exists());

    }


}

class FileUtils {
    public static void deleteDirRecur(File dir) {
        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            dir.delete();
            return;
        }
        for (File file : files) {
            deleteDirRecur(file);
        }
        dir.delete();
    }

    public static void deleteDir(File dir) {
        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            dir.delete();
            return;
        }
        while (true) {

            for (File file : files) {
                files = file.listFiles();
                if (files == null || files.length == 0)
                    dir.delete();
            }
        }
    }

    public static void printDirResur(File dir, int count) {
        File[] files = dir.listFiles();
        System.out.println();
        for (int i = 0; i < count; i++) {
            System.out.print("---");
        }
        System.out.print(dir.getName());
        if (files == null) return;
        System.out.print(":");
        for (File file : files) {
            printDirResur(file, count + 1);
        }
    }
}
